//
//  TodayViewController.m
//  UI_A段项目_iDaily Watch
//
//  Created by 刘旭 on 14-10-13.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TodayViewController.h"
#import "TodayCellOneTableViewCell.h"
#import "TodayCellTwoTableViewCell.h"
#import "TodaySecondViewController.h"
#import "TodayListViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "TodayVedio.h"
#import "TodayContent.h"
#import "UIButton+WebCache.h"
#import "MJRefresh.h"
@interface TodayViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataOne;
@property (nonatomic, retain)NSMutableArray *dataTwo;
@end

@implementation TodayViewController
- (void)dealloc
{
    [_tableView release];
    [_dataOne release];
    [_dataTwo release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self getData];
        
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self tableView1];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
}
#pragma mark - 
#pragma mark 获取数据
- (void)getData
{
    self.dataOne = [NSMutableArray array];
    AFHTTPRequestOperationManager * manager1 = [AFHTTPRequestOperationManager manager];
    [manager1 GET:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=9" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary * dic in responseObject) {
            TodayVedio *button = [[TodayVedio alloc]init];
            button.cover_landscape_hd_568h = [dic objectForKey:@"cover_landscape_hd_568h"];
            [_dataOne addObject:button];
            [_tableView reloadData];
           
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    
    
    self.dataTwo = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager2 = [AFHTTPRequestOperationManager manager];
    [manager2 GET:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=9" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary *dic in responseObject) {
            TodayContent *today = [[TodayContent alloc]init];
            today.title = [dic objectForKey:@"title"];
            today.content = [dic objectForKey:@"content"];
            [_dataTwo addObject:today];
           [_tableView reloadData];
         
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark -
#pragma mark tableview
- (void)tableView1
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    _tableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_tableView];
    //添加手势
    UISwipeGestureRecognizer * gesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(gestureAction)];
    [gesture setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.tableView addGestureRecognizer:gesture]; //添加手势到view
    [gesture release];

    //navigation黑色
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    //给navigation添加背景图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(80.0f, 0.0f, 160.0f, 44.0f)];
    imageView.image = [UIImage imageNamed:@"TodayiDailyWatch.png"];
    [self.navigationController.navigationBar addSubview:imageView];
    //左右右上角图标
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(0, 0, 30, 30);
    [button1 setBackgroundImage:[UIImage imageNamed:@"Today刷新.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventTouchUpInside];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(270, 0, 30, 30);
    [button2 setBackgroundImage:[UIImage imageNamed:@"Today列表.png"] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2Action) forControlEvents:UIControlEventTouchUpInside];
    
//    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(item1Action)];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
    self.navigationItem.leftBarButtonItem = item1;
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithCustomView:button2];
    [item1 release];
    self.navigationItem.rightBarButtonItem = item2;
    [item2 release];
}
//
- (void)refreshAction
{
    [_tableView reloadData];
    [self setupRefresh];
}
#pragma mark -
#pragma mark 刷新
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    //    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    //    [self.tableView footerEndRefreshing];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.headerPullToRefreshText = @"下拉以上弦...";
    self.tableView.headerReleaseToRefreshText = @"松开即可上弦...";
    self.tableView.headerRefreshingText = @"上弦中...";
    
    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.tableView.footerRefreshingText = @"加载中";
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        //[self.tableView1 reloadData];
        
        
        [_tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView headerEndRefreshing];
    });
}

- (void)button2Action
{
    TodayListViewController *vc = [[TodayListViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}
- (void)item1Action
{
    NSLog(@"1111");
}
#pragma mark - 
#pragma mark cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 250 + 50;
    }
    else
        return 160;
}
#pragma mark - 
#pragma mark cell number
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataOne.count  == 0 || _dataTwo.count == 0) { //当数组为空 不创建cell。
        return 0;
    }
    return 2;
}
#pragma mark - 
#pragma mark UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        static NSString *identifier = @"cellOne";
        TodayCellOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[TodayCellOneTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
          cell.selectionStyle = NO;
        NSURL *url = [NSURL URLWithString:[_dataOne[0]cover_landscape_hd_568h]];
        [cell.button setBackgroundImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"t11.png"]];
       
        return cell;
    }
    else if(indexPath.row == 1)
    {
        static NSString *identifier = @"cellTwo";
        TodayCellTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[TodayCellTwoTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        cell.selectionStyle = NO;//点击清灰
        cell.title1.text = [_dataTwo[indexPath.row - 1]title];
        cell.content1.text = [_dataTwo[indexPath.row - 1]content];
        return cell;
    }
    else
        return 0;
}
#pragma mark -
#pragma mark clean
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//}
#pragma mark -
#pragma mark 清扫手势

- (void)gestureAction
{
    TodaySecondViewController *vc = [[TodaySecondViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
